package com.dart.junit;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

//@ContextConfiguration(locations={"classpath:spring/spring-base.xml", "classpath:spring/spring-datasource.xml", "classpath:spring/spring-mybatis.xml"})
//@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring/spring-mvc.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class BaseControllerJunit {

	@Autowired
	protected WebApplicationContext wc;

	protected MockMvc mockMvc;

	// 模拟request,response
	protected MockHttpServletRequest request;
	protected MockHttpServletResponse response;

	@Before
	public void setup() {
		this.mockMvc = MockMvcBuilders.standaloneSetup(wc).build();  
		this.request = new MockHttpServletRequest();
		this.request.setCharacterEncoding("UTF-8");
		this.response = new MockHttpServletResponse();
	}
	
	
	/*测试以普通请求参数发送的请求*/  
    /*@Test  
    public void testSave() throws Exception {  
        mockMvc.perform(post("/user/save").param("id", "123").param("username", "you")).andExpect(status().isOk()).andExpect(view().name("result"));  
    }*/  
    /*测试将数据以JSON格式写入请求体发送的请求*/  
    /*@Test  
    public void testGet() throws Exception {  
        mockMvc.perform(post("/user/getUser").contentType(JsonUtil.APPLICATION_JSON_UTF8).content(JsonUtil.convertObjectToJsonBytes(new User(22,"werwr")))).andExpect(status().isOk());  
    } */ 
    /*测试将数据以JSON格式写入请求体发送的请求*/  
    /*@Test  
    public void testGetAll() throws IOException, Exception {  
        List<User> list=new ArrayList<User>();  
        list.add(new User(23,"你爱我"));  
        list.add(new User(25,"我不爱你"));  
        mockMvc.perform(post("/user/getUsers").contentType(JsonUtil.APPLICATION_JSON_UTF8).content(JsonUtil.convertObjectToJsonBytes(list))).andExpect(status().isOk());  
    }  */
    /*测试文件上传发送的请求*/  
	/*@Test
	public void testUpload() throws Exception {
		MockMultipartFile file = new MockMultipartFile("file", "orig.txt", null, "bar".getBytes());
		mockMvc.perform(fileUpload("/user/upload").file(file)).andExpect(status().isOk());
	}*/

}
